﻿using System;
using System.Collections.Generic;
using System.Windows;
using Sonce.Editor.Data;

namespace Sonce.Editor
{
	/// <summary>
	/// ElementTag adds element properties to an image.
	/// </summary>
	public partial class ElementTag : ITag
	{
		private Guid uniqueId = Guid.NewGuid();

		public int Id
		{
			get;
			set;
		}

		public Guid UniqueId
		{
			get
			{
				return this.uniqueId;
			}
		}

		private List<NodeTag> nodes = null;
		public List<NodeTag> Nodes
		{
			get
			{
				return nodes;
			}
			set
			{
				this.nodes = value;
			}
		}

		public string Name
		{
			get;
			set;
		}

		public string Value
		{
			get;
			set;
		}

		public string Label
		{
			get;
			set;
		}

		public string Source
		{
			get;
			set;
		}

		public string Xaml
		{
			get;
			set;
		}

		#region Constructor
		public ElementTag()
		{
		}
		#endregion

		#region Methods
		/// <summary>
		/// Gets ElementPrototypeId from name.
		/// </summary>
		/// <returns></returns>
		public int GetPrototypeId()
		{
			int result = (int)ElementPrototypes.Unknown;
			if (!string.IsNullOrEmpty(this.Name))
			{
				//Library xml has Name attribute which should correspond to name in ElementPrototypes enum
				if (Enum.IsDefined(typeof(ElementPrototypes), this.Name))
				{
					result = (int)(ElementPrototypes)Enum.Parse(typeof(ElementPrototypes), this.Name, true);
				}
			}
			return result;
		}

		/// <summary>
		/// Loads nodes from a visual element.
		/// </summary>
		/// <param name="visual"></param>
		/// <returns></returns>
		public List<NodeTag> GetNodes(FrameworkElement visual)
		{
			//TODO: Get relative positions (according to visual position's origin)
			this.nodes = new List<NodeTag>();
			List<Point> positions = LogicManager.FindNodePositions(visual);
			foreach (Point position in positions)
			{
				this.nodes.Add(new NodeTag() { Position = position });
			}
			return this.nodes;
		}
		#endregion
	}
}
